<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","paktrasl");
$FileName      = "ais_setup_company.php";
$FileTemplate  = "ais_setup_company_form.html";
$SubHeaderTitle= "Setup Company Profile";
$TableName     = "$DBGeneral.tblcompany";
$HTMLFormName  = "ProfileForm";

$FormAction    = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType   = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields    = array("paktrasl"=>"","paktrnam"=>"","sdcode"=>"","paaddr"=>"","pamgr"=>"","pajab"=>"","pagroup"=>"","papers"=>"","paphone"=>"","pafax"=>"","patelex"=>"","pamancost"=>"","pajabcost"=>"","pamankeu"=>"","pajabkeu"=>"","pakwtnpwp"=>"","pakwtpkp"=>"date");
buildsqlfield ("$TableName", $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name");
$FormCValue    = BuildHTMLField($FormFields,"value");

$SQLGet = "SELECT paktrasl FROM ".$TableName;
$arrProfileId = $DBConnection->dbc->get_row($SQLGetProfileId,ARRAY_A);

//fetch profile ID
$profileId = $arrProfileId['profile_id'];



//************ FORM ACTION **************
if (GetParam("form","")=="$HTMLFormName") {
	if (GetParam("Action","")=="Add") {
		FormInsert();
	}
	if (GetParam("Action","")=="Update") {
		
		list($day,$month,$year) = explode("/",GetParam("pakwtpkp",""));
		$pakwtpkp = "$year-$month-$day";
		$FormMode="Edit";
		//Edited By Krisna Wednesday 19 October 2011
		//FormUpdate();
		$SQLUpdate = "UPDATE ".$TableName." SET 
        paktrasl='".GetParam("paktrasl","")."',
        paktrnam='".GetParam("paktrnam","")."',
        sdcode='".GetParam("sdcode","")."',
        paaddr='".GetParam("paaddr","")."',
        pamgr='".GetParam("pamgr","")."',
        pajab='".GetParam("pajab","")."',
		pagroup='".GetParam("pagroup","")."',
        papers='".GetParam("papers","")."',
        paphone='".GetParam("paphone","")."',
		pafax='".GetParam("pafax","")."',
		patelex='".GetParam("patelex","")."',
		pamancost='".GetParam("pamancost","")."',
		pajabcost='".GetParam("pajabcost","")."',
		pamankeu='".GetParam("pamankeu","")."',
		pajabkeu='".GetParam("pajabkeu","")."',
		pakwtnpwp='".GetParam("pakwtnpwp","")."',
		pakwtpkp='".$pakwtpkp."'";
		$DBConnection->dbc->query($SQLUpdate);
		
		
		/*$DBConnection->dbc->query("UPDATE ".$TableName." SET 
        paktrasl='".GetParam("paktrasl","")."',
        paktrnam='".GetParam("paktrnam","")."',
        sdcode='".GetParam("sdcode","")."',
        paaddr='".GetParam("paaddr","")."',
        pamgr='".GetParam("pamgr","")."',
        pajab='".GetParam("pajab","")."',
		pagroup='".GetParam("pagroup","")."',
        papers='".GetParam("papers","")."',
        paphone='".GetParam("paphone","")."',
		pafax='".GetParam("pafax","")."',
		patelex='".GetParam("patelex","")."',
		pamancost='".GetParam("pamancost","")."',
		pajabcost='".GetParam("pajabcost","")."',
		pamankeu='".GetParam("pamankeu","")."',
		pajabkeu='".GetParam("pajabkeu","")."',
		pakwtnpwp='".GetParam("pakwtnpwp","")."',
		pakwtpkp='".GetParam("pakwtpkp","")."'");*/
		//Edited By Krisna Wednesday 19 October 2011
		
	}
	if (GetParam("Action","")=="Delete") {
		$FormMode="Edit";
		$FormMode="Edit";
		FormDelete();
	}
	if (GetParam("Action","")=="Cancel") {
		$FormMode="Edit";
		FormCancel();
	}
}

//Field Verification 
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
$VerifyFields  = array("PAKTRASL"=>"Office Code",
					   					"PAKTRNAM"=>"Office",
					   					"PAPERS"=>"Company");

VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar);

//************ SET FORM MODE ***************
$SQLCount = "SELECT COUNT(*) FROM $TableName";
$isExist = $DBConnection->dbc->get_var($SQLCount);

if ($isExist) $FormMode = "Edit";
else $FormMode = "Add";

//******* DATABASE FORM QUERY ***********

$SQL = "SELECT * FROM $TableName ";

if ($isExist && $FormMode="Edit") {
    $SQLForm = $SQL;
    //"WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."'";
}

//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

if ($isExist) $TBS->MergeBlock("data","mysql",$SQLForm);
else $TBS->MergeBlock("data",$FormCValue);
//HTML Name
$TBS->MergeBlock("bln",$FormCName);
//HTMLGrid
//$TBS->MergeBlock("blg",$DBMysql,$SQL);
$TBS->Show();

//*************** ACTION FUNCTION *******************
function FormInsert() {
	global $DBConnection,$FileName;
	$DBConnection->Insert();
	header("Location: $FileName?PageMode=Form");
	exit;
}

function FormUpdate() {
	global $DBConnection,$FileName;
	$DBConnection->Update();
	header("Location: $FileName");
	exit;		
}

function FormDelete() {
	global $DBConnection,$FileName;
	$DBConnection->Delete("tblvendor",PRIMARY_KEY);
	header("Location: $FileName");
	exit;	
}

function FormSearch () {

}

function FormCancel () {
	global $FileName;
	header("Location: ".$FileName);
	exit;
}

//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
		global $DBConnection;
		foreach ($arrayfield as $key=>$Formatting) {
			$value = GetParam($key,"");
			if ($Formatting=="date") {
				list($day,$month,$year) = explode("/",$value);
				$value = "$year-$month-$day";
			} elseif ($Formatting=="number") $value = str_replace(",","",$value);
			$DBConnection->add_htmlfield($table,$key,$value);
		}
}
?>
